﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DoubanApiLib.EntityBase;
using DoubanApiLib.Entity;
using System.Data;

namespace DoubanApiLib.Base
{
    public  static class PageContainerExtend
    {
        /// <summary>
        ///  扩展方法尝试实例化分页容器对象
        /// </summary>
        /// <param name="dataset"></param>
        /// <returns></returns>
        public static void Setup<T>(this PageContainer<T> obj, DataSet dataset) where T : Entity<T>, new()
        {
            try
            {
                PageContainer<T> temp = PageContainer<T>.CreateInstance(dataset);
                if (temp.PageBase == null)
                {
                    obj = null;
                }
                else
                {
                    obj.SetPropertyValue("PageBase", temp.PageBase);
                    obj.SetPropertyValue("DataSource", temp.DataSource);
                }
            }
            catch
            {
                obj = null;
            }
        }
    }
    public class PageContainer<T> where T : Entity<T>,new()
    {

        /// <summary>
        /// 分页基础信息
        /// </summary>
        public PageInfo PageBase
        {
            get;
            set;
        }

        /// <summary>
        /// 数据源
        /// </summary>
        public List<T> DataSource
        {
            get;
            set;
        }
        
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get
            {
                
                return PageBase ==null? 0 : PageBase.itemsPerPage;
            }
        }

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex
        {
            get
            {
                return PageBase == null ? 0 : PageBase.startIndex;
            }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int RecordCount
        {
            get
            {
                return PageBase == null ? 0 : PageBase.totalResults;
            }
        }

        /// <summary>
        /// 页数
        /// </summary>
        public int PageCount
        {
            get
            {
                if (PageSize != 0)
                {
                    int count = RecordCount / PageSize;
                    if ((count % PageSize) > 0)
                    {
                        count++;
                    }
                    return count;
                }
                else
                {
                    return 0;
                }
            }
        }

        public PageContainer()
        {

        }

        private PageContainer(DataSet dataset)
        {
            try
            {
                DataSource = Entity<T>.CreateInstanceList(dataset);
                PageBase = Entity<PageInfo>.CreateInstance(dataset);
            }
            catch
            {
                PageBase = null;
                DataSource = null;
            }
        }

        /// <summary>
        /// 尝试实例化分页容器对象
        /// </summary>
        /// <param name="dataset"></param>
        /// <returns></returns>
        public static PageContainer<T> CreateInstance(DataSet dataset)
        {
            try
            {
                PageContainer<T> temp = new PageContainer<T>(dataset);
                if (temp.PageBase == null)
                {
                    return null;
                }
                else
                {
                    return temp;
                }
            }
            catch
            {
                return null;
            }
        }

    }
}
